package com.eastfair.common.controller.micetother;

import cn.hutool.core.collection.CollectionUtil;
import com.eastfair.common.consts.CrmConstants;
import com.eastfair.common.dto.*;
import com.eastfair.common.service.CrmSvcInfSyncService;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.venuebooking.api.ServiceProviderFeign;
import com.eastfair.venuebooking.api.ServiceProviderUserFeign;
import com.eastfair.venueservice.dto.ServiceProviderUserDTO;
import com.eastfair.venueservice.enumeration.ServiceProviderTypeEnum;
import com.eastfair.venueservice.vo.ServiceProviderUserVO;
import com.eastfair.venueservice.vo.ServiceProviderVO;
import core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Crm 服务商同步任务.
 *
 * @author dqq
 * @date 2023 /2/13
 */
@Component
@Slf4j
public class CrmSvcInfSyncTask {

    @Value("${com.eastfair.projectId}")
    private Long projectId;

    @Resource
    private ServiceProviderFeign serviceProviderFeign;


    @Resource
    private ServiceProviderUserFeign serviceProviderUserFeign;


    @Resource
    private CrmSvcInfSyncService crmSvcInfSyncService;

    /**
     * 同步Crm服务商.
     */
    // @Scheduled(cron = "0/10 * * * * *")
    @XxlJob("syncSvcInfo")
    public void syncSvcInfo() {
        log.info("syncSvcInfo - 同步Crm服务商");
        ContextUtil.setProjectId(projectId);
        GmApiSvcInfPageDtos gmApiSvcInfPageDtos = new GmApiSvcInfPageDtos();
        R<List<ServiceProviderVO>> r = serviceProviderFeign.queryCrmSyncServiceProvider();
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        List<ServiceProviderVO> serviceProviderVOList = r.getData();
        if (serviceProviderVOList == null || serviceProviderVOList.isEmpty()) {
            log.info("syncSvcInfo - 没有待同步的服务商");
            return;
        }
        List<GmApiSvcInfPageDto> gmApiSvcInfPageDtoList = Lists.newArrayList();
        for (ServiceProviderVO serviceProviderVO : serviceProviderVOList) {
            GmApiSvcInfPageDto gmApiSvcInfPageDto = new GmApiSvcInfPageDto();
            gmApiSvcInfPageDto.setCrdtNo(serviceProviderVO.getUscc());
            gmApiSvcInfPageDto.setCrdtTpcd(CrmConstants.CRDT_TPCD);

            gmApiSvcInfPageDto.setCtcPsn(serviceProviderVO.getPrincipalName().length() > 9 ? serviceProviderVO.getPrincipalName().substring(0, 9) : serviceProviderVO.getPrincipalName());
            gmApiSvcInfPageDto.setCtcTel(serviceProviderVO.getPrincipalMobile());

            gmApiSvcInfPageDto.setDtSrc(CrmConstants.DT_SRC);
            gmApiSvcInfPageDto.setSvcId(serviceProviderVO.getId() + "");
            gmApiSvcInfPageDto.setSvcNm(serviceProviderVO.getCompanyName());
            if (ServiceProviderTypeEnum.BUILD_SERVICE_PROVIDER.getCode().equals(serviceProviderVO.getServiceProviderType())
                    || ServiceProviderTypeEnum.HOME_SERVICE_PROVIDER.getCode().equals(serviceProviderVO.getServiceProviderType())) {
                gmApiSvcInfPageDto.setSvcTp("1");
            }
            if (ServiceProviderTypeEnum.MAIN_TRANSPORT_SERVICE_PROVIDER.getCode().equals(serviceProviderVO.getServiceProviderType())) {
                gmApiSvcInfPageDto.setSvcTp("2");
            }
            gmApiSvcInfPageDtoList.add(gmApiSvcInfPageDto);
        }
        gmApiSvcInfPageDtos.setDtos(gmApiSvcInfPageDtoList);
        crmSvcInfSyncService.syncSvcInfo(gmApiSvcInfPageDtos);
        //同步组织信息
        syncOrganization(serviceProviderVOList);
        // 更新服务商为已同步
        List<Long> ids = serviceProviderVOList
                .stream()
                .map(ServiceProviderVO::getId)
                .collect(Collectors.toList());
        serviceProviderFeign.updateCrmHasSync(ids);
    }


    /**
     * 同步组织信息
     */
    public void syncOrganization(List<ServiceProviderVO> serviceProviderVOList) {
        if (CollectionUtil.isEmpty(serviceProviderVOList)) {
            return;
        }
        //设置项目id
        ContextUtil.setProjectId(projectId);
        GmApiSvcInfOrganizationDtos gmApiSvcInfOrganizationDtos = new GmApiSvcInfOrganizationDtos();
        List<GmApiOrganizationDto> gmApiSvcInfPageDtoList = Lists.newArrayList();
        for (ServiceProviderVO serviceProviderVO : serviceProviderVOList) {
            GmApiOrganizationDto gmApiOrganizationDto = new GmApiOrganizationDto();
            gmApiOrganizationDto.setCrdtNo(serviceProviderVO.getUscc());
            gmApiOrganizationDto.setCstNm(serviceProviderVO.getCompanyName());
            gmApiSvcInfPageDtoList.add(gmApiOrganizationDto);
        }
        gmApiSvcInfOrganizationDtos.setDtos(gmApiSvcInfPageDtoList);
        if (CollectionUtil.isEmpty(gmApiSvcInfOrganizationDtos.getDtos())) {
            return;
        }
        crmSvcInfSyncService.syncOrganizationInfo(gmApiSvcInfOrganizationDtos);
    }


    /**
     * 同步Crm服务商用户
     */
//    @Scheduled(cron = "0/10 * * * * *")
    @XxlJob("syncSvcOrgUserInfo")
    public void syncSvcOrgUserInfo() {
        log.info("syncSvcInfo - 同步Crm服务商用户");
        ContextUtil.setProjectId(projectId);
        GmApiSvcInfOrganizationUserDtos gmApiSvcInfOrganizationUserDtos = new GmApiSvcInfOrganizationUserDtos();
        R<List<ServiceProviderUserVO>> r = serviceProviderUserFeign.queryCrmSyncServiceProviderUser();
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        List<ServiceProviderUserVO> serviceProviderUserVOList = r.getData();
        if (serviceProviderUserVOList == null || serviceProviderUserVOList.isEmpty()) {
            log.info("syncSvcInfo - 没有待同步的服务商用户");
            return;
        }
        List<GmApiOrganizationUserDto> gmApiOrganizationUserDtos = Lists.newArrayList();
        for (ServiceProviderUserVO serviceProviderUserVO : serviceProviderUserVOList) {
            GmApiOrganizationUserDto gmApiOrganizationUserDto = new GmApiOrganizationUserDto();
            gmApiOrganizationUserDto.setCrdtNo(serviceProviderUserVO.getUscc());
            gmApiOrganizationUserDto.setCrdtNoPs(serviceProviderUserVO.getCardNumber());

            gmApiOrganizationUserDtos.add(gmApiOrganizationUserDto);
        }
        gmApiSvcInfOrganizationUserDtos.setDtos(gmApiOrganizationUserDtos);
        crmSvcInfSyncService.syncOrganizationUserInfo(gmApiSvcInfOrganizationUserDtos);
        //更新组织用户已同步
        ServiceProviderUserDTO serviceProviderUserDTO = new ServiceProviderUserDTO();
        serviceProviderUserDTO.setUserIdList(serviceProviderUserVOList.stream().map(ServiceProviderUserVO::getUserId).collect(Collectors.toList()));
        serviceProviderUserFeign.updForCmsUserSyncupdSync(serviceProviderUserDTO);

    }

}